/**
 * 
 */
package idv.takeshi.software.productline.bookstore.presentation.web.tag;

import idv.takeshi.software.productline.bookstore.domain.model.category.Category;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Show navigation categories to jsp page.
 * @author takeshi
 *
 */
public class NavCategoriesTagHandler extends AbstractSimpleTagSupport {
	
	private static final Log log = LogFactory.getLog(NavCategoriesTagHandler.class);
	
	private String id;
	
	private String className;
	
	private Category root;
	
	private String uri;
	
	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
	 */
	@Override
	public void doTag() throws JspException, IOException {
		// TODO Auto-generated method stub
		final String msg = "generate nav categories content error";
		try {
			super.doTag();
			JspWriter writer = this.getJspContext().getOut();
			writer.print("<ul");
			if(StringUtils.isNotBlank(this.id))
				writer.print(" id=\"" + this.id  + "\"");
			writer.print(" class=\"" + this.className  + "\">");
			
			writer.print("");
			if(root.hasSubCategories()){
				String categoryName = null;
				for(Category category : this.root.getSubCategories()){
					categoryName = category.getName().getText(this.getLocaleString());
					writer.print("<li>");
					writer.print("<a href=\"" + this.uri + category.getId() + "\"");
					writer.print(" title=\"" + categoryName + "\">");
					writer.print("<span>" + categoryName + "</span></a>");
					writer.println("</li>");
				}
			}
			this.getJspBody().invoke(writer);
			writer.println("</ul>");
		
		} catch (JspException e) {
			log.error(msg, e);
			throw e;
		} catch (IOException e) {
			log.error(msg, e);
			throw e;
		}
	}


	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}


	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}


	/**
	 * @return the className
	 */
	public String getClassName() {
		return className;
	}


	/**
	 * @param className the className to set
	 */
	public void setClassName(String className) {
		this.className = className;
	}


	/**
	 * @return the root
	 */
	public Category getRoot() {
		return root;
	}


	/**
	 * @param root the root to set
	 */
	public void setRoot(Category root) {
		this.root = root;
	}


	/**
	 * @return the uri
	 */
	public String getUri() {
		return uri;
	}


	/**
	 * @param uri the uri to set
	 */
	public void setUri(String uri) {
		this.uri = uri;
	}
	

}
